<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/9
 * Time: 17:53
 */
namespace app\api\controller;
use think\Db;
//定时任务
class Timing extends Base {
    //礼物没有领取24小时退还
    public function index(){
        $time =time();
        $list = Db::table('up_friend')->where('friend_status',1)->select();
        foreach ($list as $key=>$vo){
            $hours = strtotime(date('Y-m-d H:i:s', strtotime ("+24 hours", strtotime($vo['friend_time']))));
            if($time>$hours){
                Db::table('up_u_user')->where('user_id',$vo['user_id'])->setInc('user_balance',$vo['friend_glod']);
                Db::table('up_friend')->where('friend_id',$vo['friend_id'])->delete();
            }
        }
    }
    //红包24小时没有领取完退还
    public function rob(){
        $time =time();
        $list = Db::table('up_envelopes')->where('envelopes_status',1)->select();
        foreach ($list as $key=>$vo){
            $hours = strtotime(date('Y-m-d H:i:s', strtotime ("+24 hours", strtotime($vo['envelopes_time']))));
            if($time>$hours){
                Db::table('up_u_user')->where('user_id',$vo['user_id'])->setInc('user_balance',$vo['surplus_price']);
                //更新男性用户消费记录  todo
                Db::table('up_u_user')->where('user_id',$vo['user_id'])->setDec('user_consume',$vo['surplus_price']);

                Db::table('up_envelopes')->where('envelopes_id',$vo['envelopes_id'])->setField('envelopes_status',2);
                Db::table('up_envelopes')->where('envelopes_id',$vo['envelopes_id'])->setField('surplus_price',0);
            }
        }
    }
    
    /**
     * 修改聚会状态&插入消息表
     */
    public function partyStatus(){
        $time = date('Y-m-d H:i',strtotime('-30 minute'));
        $list = Db::table('up_part')
            ->where('p_status', 1)
            ->where('p_start_time', '<=', $time)
            ->field('part_id, part_name, p_user_id')
            ->select();
        if($list){
            foreach($list as $k => $v){
                $map['part_id'] = $v['part_id'];
                $map['m_status'] = 2;
                $m_user_id = Db::table('up_part_member')->where($map)->column('m_user_id');
                $list1 = [];
                foreach ($m_user_id as $key=>$vo){
                    $v1 = (string)$vo;
                    array_push($list1, $v1);
                }
                $code=2;
                $str = array('msg'=>'您的' . $v['part_name'] . '已经开始','type'=>9,'part_id'=>$v['part_id']);
                $str1 ='您的' . $v['part_name'] . '已经开始';
                try{
                    $this->Jpush1(1, $list1, $str1, $str,$code);
                }catch(\Exception $e){

                }
                // 修改聚会状态
                Db::table('up_part')->where('part_id', $v['part_id'])->setField('p_status', 2);
                // 消息表
                $notice = [
                    'notice_title' => '您创建的' . $v['part_name'] . '已经开始',
                    'notice_receive_user_id' => ',0,' . $v['p_user_id'] . ',',
                    'notice_read_user_id' => ',0,',
                    'notice_del_user_id' => ',0,',
                    'notice_type' => 2,
                    'notice_party_id' => $v['part_id'],
                    'notice_time' => date('Y-m-d H:i:s')
                ];
                Db::table('up_notice')->insert($notice);
            }
        }
    }

    /***
     * 俱乐部发放金币
     */
    public function clubGold(){
        $time = date('H:i',time());
        $map['a.club_time'] = $time;
        $map['a.club_status'] = 1;
        //查询俱乐部创建人账户余额和这个俱乐部几点发放金币，发放多少金币
        $res = Db::table('up_club')->alias('a')
            ->join('up_gold ug','ug.gold_id = a.gold_id','left')
            ->join('up_u_user uuu','uuu.user_id = a.club_user_id','left')
            ->field('ug.gold_price,uuu.user_balance,a.*')
            ->where($map)->select();
        foreach ($res as $key=>$vo){//循环查询这个俱乐部有多少女性用户
            $where['a.club_id'] = $vo['club_id'];
            $where['a.m_status'] = 2;
            $where['uuu.user_gender'] = 2;
            $where['a.m_type'] = 1;
            $userList = Db::table('up_club_member')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
                ->where($where)
                ->column('uuu.user_id');
            //总共有多少女性用户
            $num = count($userList);
            if(($num*$vo['gold_price'])>$vo['user_balance']){//如果发放的总金币数量小于账户余额
                if($vo['club_state']==1){//如果上次发放金币正常的话，就变成发放奖励不正常
                    Db::table('up_club')->where('club_id',$vo['club_id'])->setField('club_state',2);
                }
            }else{//如果账户余额大于发放金币的总数量的话,就正常发放金币奖励
                //插入俱乐部发放记录表
                $data['user_id'] = $vo['club_user_id'];
                $data['club_id'] = $vo['club_id'];
                $data['price'] = $num*$vo['gold_price'];
                $data['f_time'] = date('Y-m-d H:i:s',time());
                Db::table('up_club_species')->insertGetId($data);
                //更新女性用户账号余额和版单积分
                Db::table('up_u_user')->where('user_id','IN',$userList)->setInc('user_balance',$vo['gold_price']);
                Db::table('up_u_user')->where('user_id','IN',$userList)->setInc('user_integral',$vo['gold_price']);
                if($vo['club_state']==2){//如果上次发放金币异常的话，就变成发放奖励正常
                    Db::table('up_club')->where('club_id',$vo['club_id'])->setField('club_state',1);
                }
                //更新俱乐部累计发放金币
                Db::table('up_club')->where('club_id',$vo['club_id'])->setInc('total_price',$num*$vo['gold_price']);
            }
        }
    }
}